//
//  VisitorLoginView.swift
//  WbHenryTest
//
//  Created by 张洪毓 on 15/11/11.
//  Copyright © 2015年 张洪毓. All rights reserved.
//

import UIKit
@objc
protocol VisitorLoginViewDelegate: NSObjectProtocol {
    
    optional func visitorWillLogin()
    optional func visitorWillRegister()
}

class VisitorLoginView: UIView {


    weak var visitorDelegate: VisitorLoginViewDelegate?
    
    func loginDidClick() {
        visitorDelegate?.visitorWillLogin!()
    }
    
    func registerDidClick() {
        visitorDelegate?.visitorWillRegister!()
    }
    
    override init(frame: CGRect) {
        super.init(frame: CGRectZero)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUIInfo(imageName: String?,title: String) {
        circleView.hidden = false
        tipLabel.text = title
        if imageName != nil {
            iconView.hidden = true
            circleView.image = UIImage(named: imageName!)
            bringSubviewToFront(circleView)
        }else{
            startAnimation()
        }
    }
    
    private func startAnimation() {
        let animation = CABasicAnimation()
        animation.keyPath = "transform.rotation"
        animation.duration = 6
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        circleView.layer.addAnimation(animation, forKey: nil)
    }
    
    func setupUI() {
        addSubview(circleView)
        addSubview(iconView)
        addSubview(backView)
        addSubview(tipLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //布局圆圈的约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -60))
        //布局大图背景的约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1, constant: 0))
        //描述文字的约束
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        //登录按钮的约束
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        
        //注册按钮的约束
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        //渐变图片backView的约束
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-35)-[loginButton]", options: [], metrics: nil, views: ["backView":backView,"loginButton":loginButton]))
        loginButton.addTarget(self, action: "loginDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        registerButton.addTarget(self, action: "registerDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        backgroundColor = UIColor.init(white: 0.93, alpha: 1)

    }
    
    //加载所有控件
    private lazy var circleView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var backView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var tipLabel:UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.lightGrayColor()
        label.numberOfLines = 0
        return label
    }()
    
    private var loginButton: UIButton = {
        let loginBtn = UIButton()
        loginBtn.setTitle("登录", forState: UIControlState.Normal)
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        loginBtn.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        return loginBtn
    }()
    
    private var registerButton: UIButton = {
        let registerButton = UIButton()
        registerButton.setTitle("注册", forState: UIControlState.Normal)
        registerButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        registerButton.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        return registerButton
    }()
    
}
